package com.liu.sx.controller;

import com.liu.sx.common.entity.PageResult;
import com.liu.sx.common.entity.Result;
import com.liu.sx.common.status.CodeEnum;
import com.liu.sx.entity.Driver;
import com.liu.sx.service.DriverService;
import com.liu.sx.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/driver")
public class DriverController {

    @Autowired
    private DriverService  driverService;

    @GetMapping
    public Result findAll(int page, int rows , String query){

        Page<Driver> pages = driverService.findAll(page,rows,query);

        return  new Result(true, CodeEnum.OK.getCode(),"查询成功",new PageResult<Driver>(pages.getTotalElements(),pages.getContent()));
    }


    @PostMapping("/{UserId}")
    public List<Map<String,Object>> findDriversByUser(@PathVariable  int UserId){
        List<Driver> drivers= new ArrayList<>();
        List<Map<String,Object>> result = new ArrayList<>();
        drivers=driverService.findByUserId(UserId);

        for(Driver driver :drivers){
            Map map =new HashMap();
            map.put("id",driver.getId());
            map.put("name",driver.getAddress());
            map.put("image","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1587032030220&di=bfc40a54c3c376905f4ff044a149f6aa&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F295%2F41%2F0E87XZ5MPO7J_3epECXX_600.jpg");
            map.put("count",driver.getCount());
            map.put("x",driver.getX());
            map.put("y",driver.getY());

           String datesrt= DateTimeUtil.DateToString(driver.getCreateTime());
            System.out.println("=============="+datesrt);

            map.put("dateTime",datesrt);


            result.add(map);
        }

        return result;
    }
}
